﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace App2Wpf.Views
{
    /// <summary>
    /// CanvasCardUC.xaml 的交互逻辑
    /// </summary>
    public partial class CanvasCardUC : UserControl, ICardUC
    {
        public int ID { get; set; }
        public nameip connectNameip { get; set; }

        public System.Net.IPAddress iPAddress;

        ViewerCanvas view;
        public CanvasCardUC(nameip serverinfo)
        {
            InitializeComponent();

            initLoading();

            connectNameip = serverinfo;
        }

        private void Close_Button_Click(object sender, RoutedEventArgs e)
        {
            view.session.CloseSession();
            App2Wpf.ViewModels.ServiceLocator.rightWrapVM.removeCard(this);
        }

        public void initLoading()
        {
            LoadingG.Visibility = Visibility.Visible;
            MiniWindow.Visibility = Visibility.Hidden;
        }

        public void StartWork()
        {
            this.iPAddress = System.Net.IPAddress.Parse(connectNameip.ip);
            view = new ViewerCanvas(iPAddress, connectNameip);

            view.OnConnect += () =>
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    LoadingG.Visibility = Visibility.Collapsed;
                    MiniWindow.Visibility = Visibility.Visible;
                }));
            };
            view.OnRefresh += View_OnRefresh;

            Task.Run(() => view.init(iPAddress)); //for loading not wait

        }

        private void View_OnRefresh(System.Drawing.Point p, ImageSource block, Image t)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                Canvas.SetLeft(t, p.X);
                Canvas.SetTop(t, p.Y);
                //MiniWindow.Children.Add();

                MiniWindow.Children.Add(t);
            });
            t = null;
            block = null;
        }
    }
}

